package com.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.system.dao.CourseDao;
import com.system.model.Course;
import com.system.model.CoursePojo;
import com.system.util.JdbcUtil;
import com.system.util.ResultMapUtil;

public class CourseDaoImpl extends BaseDaoImpl implements CourseDao {

	public CourseDaoImpl() throws Exception {
		super();
	}
	
	/**
	 * 对查找的重复操作进行封装
	 * @return
	 */
	private ArrayList<Course> find(String sql){
		try{
			ResultSet rs=this.templateQuery(sql);
			boolean find = false;
			ArrayList<Course> courses = new ArrayList<>();
			while(rs.next()) {
				Course course = new Course();
				Map<String,String> map=ResultMapUtil.getResultMap(rs);
				try{
					BeanUtils.populate(course, map);
					courses.add(course);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(!find) {
				return null;
			}
			return courses;	
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<CoursePojo> findAllCourse() {
		try{
			String sql="select teacher.teachername,course.coursename from teacher,course where teacher.id=course.teacher_id";
			ResultSet rs=this.templateQuery(sql);
			List<CoursePojo> courses=new ArrayList<>();
			while(rs.next()){
				CoursePojo course=new CoursePojo();
				Map<String,String> map=ResultMapUtil.getResultMap(rs);
				try{
					BeanUtils.populate(course, map);
					courses.add(course);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			return courses;
		}
		catch(Exception e){
			return null;
		}
	}
	@Override
	public ArrayList<Course> findAll(){
		String sql="select * from "+"course";
		ArrayList<Course> courses=find(sql);
		JdbcUtil.getInstance().free(conn);
		return courses;
	}
	@Override
	public Course findByName(String courseName) {
		String sql="select * from "+"course"+" where coursename="+courseName;
		ArrayList<Course> courses=find(sql);
		//如果查询为null
		if(courses == null) {
			return null;
		}
		Course course = courses.get(0);
		JdbcUtil.getInstance().free(conn);
		return course;
	}

	@Override
	public boolean add(Course course) {
		String sql="insert into "+"course(teacher_id,coursename"+
				")"+ " values"+"("+
				"'"+course.getTeacher_id()+"'"+
				","+"'"+course.getCoursename()+"'"+")";
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public boolean deleteCourses(String[] names) {
		try{
			for(String name:names){
				String sql="delete from "+"course"+" where coursename="
						+"'"+name+"'";
				this.templateUpdate(sql);
			}
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public boolean modify(Course course) {
		String sql="update course set teacher_id="+"'"+course.getTeacher_id()+"'"+","
				+ " coursename="+"'"+course.getCoursename()+"'"
				+" where coursename="+"'"+course.getCoursename()+"'";
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public Integer findId(String coursename, Integer tea_id) {
		String sql="select id from course where teacher_id= "+tea_id
				+" and coursename="+"'"+coursename+"'";
		try {
			ResultSet rs=this.templateQuery(sql);
			int res = -1;
			if(rs.next()){
				res=(Integer)rs.getObject("id");
			}
			return res;
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<String> listCourse(Integer tea_id){
		String sql="select coursename from course where teacher_id="+tea_id;
		try {
			ResultSet rs=this.templateQuery(sql);
			List<String> list=new ArrayList<>();
			while(rs.next()){
				String coursename=rs.getString(1);
				list.add((coursename));
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
	}

	
}
